package com.zy.web.demo.duoxiancheng.demo34;

import java.util.Calendar;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author <a href="mailto:zhouyong@cqyyt.com">zy</a>
 * @version 1.0
 * @date 2020/10/12 11:06
 */
public class Service34 {
    
    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    
    public void waitMethod(){
        lock.lock();
        try {
            Calendar calendarRef =  Calendar.getInstance();
            calendarRef.add(Calendar.SECOND,10);
            System.out.println("wait begin:"+System.currentTimeMillis());
            condition.awaitUntil(calendarRef.getTime());
            System.out.println("wait end:"+System.currentTimeMillis());
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void notifyMethod(){
        lock.lock();
        try {
            Calendar calendarRef =  Calendar.getInstance();
            calendarRef.add(Calendar.SECOND,10);
            System.out.println("notify begin:"+System.currentTimeMillis());
            condition.signalAll();
            System.out.println("notify end:"+System.currentTimeMillis());
        }finally {
            lock.unlock();
        }
    }

}
